package cn.yjr.fragment;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import cn.edu.pku.tools.BitmapHelper;
import cn.edu.pku.tools.YJRHttpClient;
import cn.edu.pku.tools.FileHelper;
import cn.edu.pku.tools.SharedPreferencesHelper;
import cn.liuyi.weiyi.AccountActivity;
import cn.liuyi.weiyi.AddDoctorActivity;
import cn.liuyi.weiyi.ChatActivity;
import cn.liuyi.weiyi.DoctorPageActivity;
import cn.liuyi.weiyi.MyBalanceActivity;
import cn.liuyi.weiyi.R;
import cn.liuyi.weiyi.TransactionRecordActivity;
import cn.liuyi.weiyi.adapter.ChatRoomAdapter;

import com.actionbarsherlock.app.SherlockFragment;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;

public class ChatRoomFragment extends SherlockFragment
  implements View.OnClickListener
{
	Context context;
	FileHelper fileHelper;
	JSONObject userData;
	JSONObject jsonInfo, doctor;
	public static JSONArray doctors;
	SharedPreferencesHelper helper;
	View view;
	ImageView add_doctor_imv;
	ListView doctor_lv;
	DisplayImageOptions options;
	protected ImageLoader imageLoader = ImageLoader.getInstance();
	public ChatRoomFragment(){
	  doctors = new JSONArray();
	}
	private void initData()
	{
		getDoctors();
	}	

	void initComponent()
	{
		add_doctor_imv = (ImageView) view.findViewById(R.id.add_doctor);
		doctor_lv = (ListView) view.findViewById(R.id.doctor_lv);
		
		add_doctor_imv.setOnClickListener(this);
		doctor_lv.setOnItemClickListener(new OnItemClickListener(){

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(getActivity(), ChatActivity.class);
				JSONObject doctor_;
				try {
					doctor_ = doctors.getJSONObject(position);
					String consultStatus = doctor_.getString("consultStatus");
					//deadline = doctor_.getLong("deadline");
					doctor = doctor_.getJSONObject("doctor");
					intent.putExtra("doctor", doctor.toString());
					intent.putExtra("consultStatus", consultStatus);
					startActivity(intent);
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
			
		});
	}

	public void onClick(View paramView)
	{
		Intent intent;
		switch (paramView.getId())
		{
		case R.id.add_doctor:
			intent = new Intent(context, AddDoctorActivity.class);
			context.startActivity(intent);
			break;
		default:
			break;
		}
	}

	public View onCreateView(LayoutInflater paramLayoutInflater, ViewGroup paramViewGroup, Bundle paramBundle)
	{
	    this.view = paramLayoutInflater.inflate(R.layout.fragment_chat_room, paramViewGroup, false);
	    this.context = getActivity();
	    this.fileHelper = new FileHelper(this.context);
	    this.helper = new SharedPreferencesHelper(this.context);
	    options = new DisplayImageOptions.Builder()
		.showImageOnLoading(R.drawable.me_jobinfo)
		.showImageForEmptyUri(R.drawable.me_appointment)
		.showImageOnFail(R.drawable.head)
		.cacheInMemory(true)
		.cacheOnDisc(true)
		.considerExifParams(true)
		.displayer(new RoundedBitmapDisplayer(100))//是否设置为圆角，弧度为多少  
		.bitmapConfig(Bitmap.Config.RGB_565)
		.build();
	    initData();
	    initComponent();
	    return this.view;
	}
  
	void setComponent(){
	  
	}
	
	private void getDoctors(){
		RequestParams params = new RequestParams(); 
		YJRHttpClient.post(context,"/patient/listFavorDoctors", params, new JsonHttpResponseHandler(){
			 @Override  
	            public void onSuccess(JSONObject data) {  
	             	jsonInfo = data;
	             	try {
						if(jsonInfo.getInt("result") == 1){
							YJRHttpClient.setCookie(context); 
							//Toast.makeText(context, "获取医生列表成功", Toast.LENGTH_SHORT).show();
							doctors = jsonInfo.getJSONArray("response");							
							doctor_lv.setAdapter(new ChatRoomAdapter(context, imageLoader, options));
							
						}else{
							Toast.makeText(context, jsonInfo.getString("info"), Toast.LENGTH_SHORT).show();
						}
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
	            }  
			 @Override  
		     public void onFinish() {  
		         // Completed the request (either success or failure)  
		     }  
		});
	}
}